package generatorGraphics.graphics;

import grammaticalClasses.nwConsole.ShowStatCur;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.StatisticalBarRenderer;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;

public class QueueCongestion2 extends Graph {
    
    public QueueCongestion2(){
        name = "Queue Congestion";
        help = "Help of Queue Congestion";
        
        configUnitOptions();
        
        commands.add("ShowStatCur");
    }
    
    private void configUnitOptions(){

        // Adicionando as unidades
        //--Não existe definição de Opções
        
        //Adicionando as opções
        //--Não existe definição de Opções
    }
    
    @Override
    protected void createGraph(String unit){
        
        int indice;
        
//        if(search == null){
//            search = new SearchCommands(commands);
//            answer = (ArrayList<Object>) search.getCommandsAnswer();
//        }
        
        switch (unit){
            case "":
                indice = search.getAnswer(commands, "ShowStatCur");
                chartPanel = new ChartPanel(buildGraph((ArrayList<ShowStatCur>) answer.get(indice)));
                chartPanel.setBackground(Color.LIGHT_GRAY);
                chartPanel.setPreferredSize(new Dimension(700, 350)); 
                chartPanel.setDomainZoomable(false);
                chartPanel.setRangeZoomable(false);
                
                break;
            default:
               
                chartPanel = new ChartPanel(null);
                break;
        }
    }
   
    private JFreeChart buildGraph(ArrayList<ShowStatCur> auxCur){
    
        ShowStatCur aux = auxCur.get(0);
        DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();
        
        for(int j=0; j<aux.getMoteStats().size(); j++){
            dataset.add((aux.getMoteStats().get(j).getAvq()).floatValue(),
                (aux.getMoteStats().get(j).getMxq() - 
                aux.getMoteStats().get(j).getAvq()),
                "Tamanho medio da fila", aux.getMoteStats().get(j).getId() + "Id");
        }

        CategoryAxis xAxis = new CategoryAxis("Dispositivos");
        //xAxis.setLowerMargin(0.01d); // percentage of space before first bar
        //xAxis.setUpperMargin(0.01d); // percentage of space after last bar
        //xAxis.setCategoryMargin(0.05d); // percentage of space between categories
        ValueAxis yAxis = new NumberAxis("Tamanho");
        // define the plot
        CategoryItemRenderer renderer = new StatisticalBarRenderer();
        CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer);
        JFreeChart chart = new JFreeChart("Congestionamento da pilha nos ultimos " + aux.getTime()
                + " segundos", new Font("Helvetica", Font.BOLD, 14),
                plot, true);
        chart.setBackgroundPaint(new Color(238, 238, 238));
        
        return chart;
        
    }
    
    @Override
    public ChartPanel getChartPanel() {
        
        if(search != null){
            createGraph("");
            return chartPanel;
        } else {
            return new ChartPanel(null);
        }
    }
    
}
